package com.anti.modular.sys.user.controller;
import com.anti.commom.annotation.BusinessLog;
import com.anti.commom.annotation.DataScope;
import com.anti.commom.annotation.Permission;
import com.anti.commom.api.Result;
import com.anti.commom.enums.LogAnnotionOpTypeEnum;
import com.anti.jdbc.support.page.PageParam;
import com.anti.modular.sys.user.param.SysUserParam;
import com.anti.modular.sys.user.service.SysUserService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 系统用户控制器
 */
@RestController
@RequestMapping("sysUser")
public class SysUserController {

    @Resource
    private SysUserService sysUserService;

    /**
     * 查询系统用户
     */
    @DataScope
    @GetMapping("page")
    @BusinessLog(title = "系统用户_查询", opType = LogAnnotionOpTypeEnum.QUERY)
    public Result page(PageParam pageParam,SysUserParam sysUserParam) {
        return Result.data(sysUserService.page(pageParam,sysUserParam));
    }

    /**
     * 增加系统用户
     */
    @Permission
    @DataScope
    @PostMapping("add")
    @BusinessLog(title = "系统用户_增加", opType = LogAnnotionOpTypeEnum.ADD)
    public Result add(@RequestBody @Validated(SysUserParam.add.class) SysUserParam sysUserParam) {
        sysUserService.add(sysUserParam);
        return Result.success("新增成功");
    }

    /**
     * 删除系统用户
     */
    @Permission
    @DataScope
    @PostMapping("delete")
    @BusinessLog(title = "系统用户_删除", opType = LogAnnotionOpTypeEnum.DELETE)
    public Result delete(@RequestBody @Validated(SysUserParam.delete.class) SysUserParam sysUserParam) {
        sysUserService.delete(sysUserParam);
        return Result.success("删除成功");
    }

    /**
     * 编辑系统用户
     */
    @Permission
    @DataScope
    @PostMapping("edit")
    @BusinessLog(title = "系统用户_编辑", opType = LogAnnotionOpTypeEnum.EDIT)
    public Result edit(@RequestBody @Validated(SysUserParam.edit.class) SysUserParam sysUserParam) {
        sysUserService.edit(sysUserParam);
        return Result.success("修改成功");
    }

    /**
     * 查看系统用户
     */
    @GetMapping("detail")
    @BusinessLog(title = "系统用户_查看", opType = LogAnnotionOpTypeEnum.DETAIL)
    public Result detail(@Validated(SysUserParam.detail.class) SysUserParam sysUserParam) {
        return Result.data(sysUserService.detail(sysUserParam));
    }

    /**
     * 修改状态
     */
    @Permission
    @PostMapping("changeStatus")
    @BusinessLog(title = "系统用户_修改状态", opType = LogAnnotionOpTypeEnum.CHANGE_STATUS)
    public Result changeStatus(@RequestBody @Validated(SysUserParam.changeStatus.class) SysUserParam sysUserParam) {
        sysUserService.saveChangeStatus(sysUserParam);
        return Result.success("状态修改成功");
    }

    /**
     * 授权角色
     */
    @Permission
    @DataScope
    @PostMapping("grantRole")
    @BusinessLog(title = "系统用户_授权角色", opType = LogAnnotionOpTypeEnum.GRANT)
    public Result grantRole(@RequestBody @Validated(SysUserParam.grantRole.class) SysUserParam sysUserParam) {
        sysUserService.saveGrantRole(sysUserParam);
        return Result.success("授权成功");
    }

    /**
     * 授权数据
     */
    @Permission
    @DataScope
    @PostMapping("grantData")
    @BusinessLog(title = "系统用户_授权数据", opType = LogAnnotionOpTypeEnum.GRANT)
    public Result grantData(@RequestBody @Validated(SysUserParam.grantData.class) SysUserParam sysUserParam) {
        sysUserService.saveGrantData(sysUserParam);
        return Result.success("授权成功");
    }

    /**
     * 更新信息
     */
    @PostMapping("updateInfo")
    @BusinessLog(title = "系统用户_更新信息", opType = LogAnnotionOpTypeEnum.UPDATE)
    public Result updateInfo(@RequestBody @Validated(SysUserParam.updateInfo.class) SysUserParam sysUserParam) {
        sysUserService.updateInfo(sysUserParam);
        return Result.success("修改成功");
    }

    /**
     * 修改密码
     */
    @PostMapping("updatePwd")
    @BusinessLog(title = "系统用户_修改密码", opType = LogAnnotionOpTypeEnum.UPDATE)
    public Result updatePwd(@RequestBody @Validated(SysUserParam.updatePwd.class) SysUserParam sysUserParam) {
        sysUserService.updatePwd(sysUserParam);
        return Result.success("密码修改成功");
    }

    /**
     * 拥有角色
     */
    @GetMapping("ownRole")
    @BusinessLog(title = "系统用户_拥有角色", opType = LogAnnotionOpTypeEnum.DETAIL)
    public Result ownRole(@Validated(SysUserParam.detail.class) SysUserParam sysUserParam) {
        return Result.data(sysUserService.ownRole(sysUserParam));
    }

    /**
     * 拥有数据
     */
    @GetMapping("ownData")
    @BusinessLog(title = "系统用户_拥有数据", opType = LogAnnotionOpTypeEnum.DETAIL)
    public Result ownData(@Validated(SysUserParam.detail.class) SysUserParam sysUserParam) {
        return  Result.data(sysUserService.ownData(sysUserParam));
    }

    /**
     * 重置密码
     */
    @Permission
    @PostMapping("resetPwd")
    @BusinessLog(title = "系统用户_重置密码", opType = LogAnnotionOpTypeEnum.UPDATE)
    public Result resetPwd(@RequestBody @Validated(SysUserParam.resetPwd.class) SysUserParam sysUserParam) {
        sysUserService.saveResetPwd(sysUserParam);
        return Result.success("密码重置成功");
    }

    /**
     * 修改头像
     */
    @PostMapping("updateAvatar")
    @BusinessLog(title = "系统用户_修改头像", opType = LogAnnotionOpTypeEnum.UPDATE)
    public Result updateAvatar(@RequestBody @Validated(SysUserParam.updateAvatar.class) SysUserParam sysUserParam) {
        sysUserService.updateAvatar(sysUserParam);
        return Result.success("头像修改成功");
    }

    /**
     * 导出系统用户
     */
    @Permission
    @GetMapping("export")
    @BusinessLog(title = "系统用户_导出", opType = LogAnnotionOpTypeEnum.EXPORT)
    public void export(SysUserParam sysUserParam) {
        //sysUserService.export(sysUserParam);
    }


    /**
     * 用户选择器
     */
    @Permission
    @GetMapping("selector")
    @BusinessLog(title = "系统用户_选择器", opType = LogAnnotionOpTypeEnum.QUERY)
    public Result selector(SysUserParam sysUserParam) {
        return Result.data(sysUserService.selector(sysUserParam));
    }
}
